'use strict';

/**
 * page service
 */
angular.module('sfhtHaitaoCmsApp')
	.service('PageService', function($q, Gateway, $injector) {

		// 通用的request方法返回promise对象
		var request = function (api, params, isForceUserLogin, cms) {

			var deferred = $q.defer();

			var api = $injector.get(api);

			var gateway = new Gateway(api, params);

			gateway.sendRequest(false, true)
				.then(function(data) {
					deferred.resolve(data);
				}, function(data) {
					deferred.reject(data);
				});

			return deferred.promise;
		};

		return {

			getResourceConfigs: function(resourceType, names) {

				var params = {
					resourceType: resourceType,
					names: JSON.stringify(names)
				}

				return request('sf_b2c_mall_api_cms_getResourceConfigs', params, false, true);
			},

			updateResourceConfigs: function(resourceType, value){
				var params = {
					resourceType: resourceType,
					name: resourceType,
					value:value
				}

				return request('sf_b2c_mall_api_cms_updateResourceConfig', params, false, true);
			},

			getPageList: function(periodOptions, isOnlyShowMine, searchKeywords, pageType, pagedQueryOption) {

				var params = {
					pageType: pageType,
					periodOptions: periodOptions,
					isOnlyShowMine: isOnlyShowMine,
					searchKeywords: searchKeywords,
					pagedQueryOption: JSON.stringify(pagedQueryOption)
				};

				return request('sf_b2c_mall_api_cms_getPageList', params, false, true);
			},

			createPage: function(page) {

				var params = {
					cmsPage: JSON.stringify(page)
				};

				return request('sf_b2c_mall_api_cms_createPage', params, false, true);
			},

			updatePage: function(page) {

				var params = {
					cmsPage: JSON.stringify(page)
				};

				return request('sf_b2c_mall_api_cms_updatePage', params, false, true);
			},

			getModuleList: function(moduleType) {

				var params = {
					moduleType: moduleType
				};

				return request('sf_b2c_mall_api_cms_getModuleList', params, false, true);
			},

			getPageDetail: function(pageId) {

				var params = {
					pageId: pageId
				};

				return request('sf_b2c_mall_api_cms_getPageDetail', params, false, true);
			},

			getModuleDetail: function(moduleName, pageType ,needAll) {

				var params = {
					moduleName: moduleName,
					moduleType: pageType,
					needAll:needAll
				};

				return request('sf_b2c_mall_api_cms_getModuleDetail', params, false, true);
			},

			recordModuleMove: function(pageId, pageConfig) {

				var params = {
					pageId: pageId,
					pageConfig: JSON.stringify(pageConfig)
				};

				return request('sf_b2c_mall_api_cms_recordModuleMove', params, false, true);
			},

			removeModuleFromPage: function(pageId, idInPage, idInLayout) {

				var params = {
					pageId: pageId,
					idInPage: idInPage,
					idInLayout: idInLayout
				};

				return request('sf_b2c_mall_api_cms_removeModuleFromPage', params, false, true);
			},

			editModule: function(pageId, idInPage, idInLayout, moduleConfig) {

				var params = {
					pageId: pageId,
					idInPage: idInPage,
					idInLayout: idInLayout,
					moduleConfig: JSON.stringify(moduleConfig)
				};

				return request('sf_b2c_mall_api_cms_editModuleOfPage', params, false, true);
			},

			/**
			 * 新增模块
			 * @param {[int]} pageId        页面Id
			 * @param {[int]} idInPage      [description]
			 * @param {[type]} indexInLayout [description]
			 * @param {[type]} module        [description]
			 */
			addModule: function(pageId, idInPage, indexInLayout, moduleConfig) {
				var params = {
					pageId: pageId,
					idInPageOfLayout: idInPage,
					indexInLayout: indexInLayout,
					moduleConfig: JSON.stringify(moduleConfig)
				};

				return request('sf_b2c_mall_api_cms_addModuleToPage', params, false, true);
			},

			batchRecreatePage: function(pageIds) {

				var params = {
					pageIdList: pageIds
				};

				return request('sf_b2c_mall_api_cms_batchRecreatePage', params, false, true);
			},

			batchUpdateProductData: function(pageIds){
				var params = {
					pageIdList: JSON.stringify(pageIds)
				};
				return request("sf_b2c_mall_api_cms_bantchSyncPropertyForItem", params, false, true)
			}
		}
	});

